package edu.whu.demo.dao;

import edu.whu.demo.entity.Commodity;
import edu.whu.demo.entity.Supplier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface SupplierRepository extends JpaRepository<Supplier, Long> {
    public Supplier findSupplierById(long id);

    @Modifying
    @Query(value = "delete from commodity_suppliers where suppliers_id = ?1", nativeQuery = true)
    void deleteBySupplierId(long id);

    @Query(value = "select * from supplier where id in " +
            "(select suppliers_id from commodity_suppliers where commodity_id = ?1)", nativeQuery = true)
    List<Supplier> findSupplierByCommodity(long id);

    Page<Supplier> findByNameContaining(String name, Pageable pageable);

    @Query(value = "select c from Supplier c where name = ?1")
    List<Supplier> findWithJPQLByName(String name);
}
